package com.partners.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.partners.constant.ErrorMessageEnum;
import com.partners.dao.basic.ProductContentDao;
import com.partners.dao.bo.ProductContentDO;
import com.partners.dao.dto.ProductInputDTO;
import com.partners.dao.basic.PointProductDao;
import com.partners.dao.basic.ProductClassDao;
import com.partners.dao.basic.ProductPicDao;
import com.partners.dao.bo.PointProductDO;
import com.partners.dao.bo.ProductClassDO;
import com.partners.dao.bo.ProductPicDO;
import com.partners.model.BusinessException;
import com.partners.model.Paging;
import com.partners.service.ProductService;
import com.partners.utils.TimestampUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liangyongle
 * @date 2018-12-12
 */
@Service
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Resource
    private PointProductDao pointProductDao;

    @Resource
    private ProductClassDao productClassDao;

    @Resource
    private ProductPicDao productPicDao;

    @Resource
    private ProductContentDao productContentDao;
    /**
     * 获取产品列表
     * @param paging Paging //分页数
     *        name String  //名字
     *        class_id Integer  //分类id
     *        type //1.兑换中，2.库存少，3.已下架
     *        isup Byte 是否上线
     * @return List
     */
    @Override
    public Map<String, Object> getProductList(Paging paging, String title, Integer classId, Integer type) {
        PageHelper.startPage(paging.getPage(),paging.getRows());
        Map<String,Object> allList = new HashMap<>();
        type = type !=null?type:1;
        List<Map<String,Object>> list =  pointProductDao.getProductListSelective(title,classId,type);
        Page page = (Page) list;
        Object total = page.getTotal();
        allList.put("total",total);
        allList.put("list",list);
        return allList;
    }


    /**
     * 获取产品详情
     * @param Id Integer
     * @return Object
     */
    @Override
    public Map<String,Object> getProductDetail(Integer Id) {
        PointProductDO pointProduct = pointProductDao.selectByPrimaryKey(Id);
        if(pointProduct == null){
            throw BusinessException.of(ErrorMessageEnum.RECORD_NOT_FOUND);
        }
        Map<String,Object> product = new HashMap<>();
        product.put("product",pointProduct);
        //获取产品详情
        List<ProductContentDO> productContent = productContentDao.selectByProductId(pointProduct.getId());
        if(productContent != null){
            product.put("product_content",productContent);
        }
        //获取产品图片
        List<String> productPic = pointProductDao.getProductPicList(pointProduct.getId());
        if(productPic != null){
            product.put("pic_list",productPic);
        }
        return product;
    }


    /**
     * 编辑产品
     * @param pointProductDO PointProductDO
     * @return Integer
     */
    @Override
    @Transactional
    public Integer editProduct(PointProductDO pointProductDO) {
        Integer result = 0;
        if(pointProductDO.getId() !=null){
            //编辑
            result = pointProductDao.updateByPrimaryKeySelective(pointProductDO);
        }else{
           //新增
            pointProductDO.setAddtime(TimestampUtils.timestamp());
            result = pointProductDao.insertSelective(pointProductDO);
        }
        return result;
    }


    /**
     * 删除产品
     * @param Id Integer
     * @return Object
     */
    @Override
    @Transactional
    public Integer deleteProduct(Integer Id) {
        PointProductDO pointProductDO = pointProductDao.selectByPrimaryKey(Id);
        if(pointProductDO == null){
            throw BusinessException.of(ErrorMessageEnum.RECORD_NOT_FOUND);
        }
        Integer result = pointProductDao.deleteByPrimaryKey(Id);
        //删除产品内容表
        pointProductDao.deleteContentByProductId(Id);
        //删除产品图片表
        pointProductDao.deletePicByProductId(Id);

        return result;
    }


    /**
     * 获取产品分类列表
     * @param paging Paging //分页数
     *        name String  //名字
     * @return List
     */
    @Override
    public Map<String, Object> getClassList(Paging paging, String name) {
        PageHelper.startPage(paging.getPage(),paging.getRows());
        Map<String,Object> allList = new HashMap<>();
        List<Map<String,Object>> list =  productClassDao.getProductClassSelective(name);
        Page page = (Page) list;
        Object total = page.getTotal();
        allList.put("total",total);
        allList.put("list",list);
        return allList;
    }


    /**
     * 获取产品分类详情
     * @param Id Integer
     * @return Object
     */
    @Override
    public ProductClassDO getProductClassDetail(Integer Id) {
        ProductClassDO productClassDO = productClassDao.selectByPrimaryKey(Id);
        if(productClassDO == null){
            throw BusinessException.of(ErrorMessageEnum.RECORD_NOT_FOUND);
        }
        return productClassDO;
    }

    /**
     * 编辑分类
     * @param productClassDO ProductClassDO
     * @return Integer
     */

    @Override
    public Integer editProductClass(ProductClassDO productClassDO) {
        Integer result = 0;
        if(productClassDO.getId() != null){
            //编辑
            result = productClassDao.updateByPrimaryKeySelective(productClassDO);
        }else{
            //新增
            productClassDO.setAddtime(TimestampUtils.timestamp());
            result = productClassDao.insertSelective(productClassDO);
        }
        return result;
    }

    /**
     * 删除分类
     * @param Id Integer
     * @return Integer
     */
    @Override
    public Integer deleteProductClassDetail(Integer Id) {
        ProductClassDO productClassDO = productClassDao.selectByPrimaryKey(Id);
        if(productClassDO == null){
            throw BusinessException.of(ErrorMessageEnum.RECORD_NOT_FOUND);
        }
        Integer result = productClassDao.deleteByPrimaryKey(Id);
        return result;
    }


    /** 获取子分类列表
     * @param Id Integer
     * @return list
     */
    @Override
    public List<Map<String,Object>> getProductClassChild(Integer Id) {
        List<Map<String,Object>> list = productClassDao.getProductClassChildByPid(Id);
        return list;
    }


    /** 编辑
     * @param productInputDTO ProductInputDTO
     * @return list
     */
    @Override
    @Transactional
    public Integer editsProduct(ProductInputDTO productInputDTO) {
        PointProductDO productDO  = new PointProductDO();
        productDO.setClassId(productInputDTO.getClassId());
        productDO.setTitle(productInputDTO.getTitle());
        productDO.setPic(productInputDTO.getPic());
        productDO.setContent(productInputDTO.getContent());
        productDO.setPoint(productInputDTO.getPoint());
        productDO.setStock(productInputDTO.getStock());
        productDO.setPrice(productInputDTO.getPrice());
        Integer productId = 0;
        if(productInputDTO.getId() != null){
            productDO.setId(productInputDTO.getId());
            pointProductDao.updateByPrimaryKeySelective(productDO);
            productId = productInputDTO.getId();
        }else{
            productDO.setAddtime(TimestampUtils.timestamp());
            pointProductDao.insertSelective(productDO);
            productId = productDO.getId();
        }
        //删除所有的照片
       productPicDao.deleteByProductId(productInputDTO.getId());
        //重新添加进去
       for (Map<String,Object> map:productInputDTO.getPiclist()){
           ProductPicDO productPicDO = new ProductPicDO();
           productPicDO.setPic((String) map.get("pic"));
           productPicDO.setProductId(productId);
           productPicDO.setAddtime(TimestampUtils.timestamp());
           productPicDao.insert(productPicDO);
       }
        return 1;
    }


    /**
     * 产品上下架
     * @param id Integer up Integer
     * @return Integer
     */
    @Override
    @Transactional
    public void editProductUp(Integer id, Integer up) {
        //查找当前产品是否存在
        PointProductDO pointProductDO = pointProductDao.selectByPrimaryKey(id);
        if(pointProductDO == null){
            throw BusinessException.of(ErrorMessageEnum.PRODUCT_NOT_FOUND);
        }
        PointProductDO pointProduct = new PointProductDO();
        pointProduct.setId(id);
        pointProduct.setIsup(up.byteValue());
        pointProductDao.updateByPrimaryKeySelective(pointProduct);
    }


}
